﻿$(document).ready(function () {
    "use strict";
    var iscreate = true;
    var opt = {
        controls: [
            {
                name: "materialmodal",
                type: "modal",
                body: [
                    {
                        type: "gridtable",
                        name: "modalgridmaterial"
                    }
                ]
            },
            {
                type: "row",
                controls: [{
                    type: "header", title: "Test Coba", breadcrumb: "mmpur",
                    toolbarbutton: [
                                { type: "btn", name: "btnCreate", text: "create", color: "blue", icon: "plus", onClick: create },
                                { type: "btn", name: "btnEdit", text: "Edit", color: "green", icon: "edit", onClick: edit },
                                { type: "btn", name: "btnDelete", text: "Delete", color: "red", icon: "times", onClick: deleted },
                                { type: "btn", name: "btnSave", text: "Save", color: "purple", icon: "save", cls: "hide", onClick: save },
                                { type: "btn", name: "btnCancel", text: "Cancel", color: "yellow", icon: "undo", cls: "hide", onClick: cancel }
                    ]
                }
                ]
            },
        {
            type: "row",
            controls: [
                {
                    type: "portlet", name: "pnlfilter", color: "green", title: "Test Filter", icon: "home", hidetool: ["remove", "config"],
                    body: [
                        {
                            type: "formgroup",
                            name:'fgsatu',
                            controls: [
                                   { name: "fltrKdPo", type: "textbox", label: "No. Po", cls: "col-md-3", placeholder: "Kode Barang" }

                            ]
                        },
                            {
                                type: "formgroup",
                                controls: [
                                    { name: "fltrKdSupp", type: "textbox", label: "Kd. Supplier", cls: "col-md-5", placeholder: "Nama barang" }
                                ]
                            },
                            {
                                type: "formgroup",
                                controls: [
                                        { name: "fltrPeriode", type: "daterange", label: "Tanggal", cls: "col-md-5", placeholder: "Barcode" }
                                ]
                            },
                            {
                                type: "formgroupbtn",
                                controls: [
                                        { name: "btnSearch", type: "btn", color: "blue", text: "Search", icon: "search", onClick: refreshgrid }

                                ]
                            },
                            {
                            type: "gridtable",
                            name: "gridsupplier",
                            gridopt: {
                                url: "api/mdm/grid/supplier",
                                name: "gridsupplier",
                                checkbox: true,
                                params:{},// data,
                                key: "KdSupp",
                                filter: true,
                                columns: [
                                { mData: "KdSupp", sTitle: "Kode Supplier " },
                                { mData: "NmSupp", sTitle: "Nama Supplier" },
                                { mData: "Phone1", sTitle: "Telp" },
                                { mData: "Email", sTitle: "Email" },
                                { mData: "Addr", sTitle: "Alamat" }

                                ],
                                callback: function (data) {
                                    EditItem(data[1]);
                                }
                            }
                            },
                            {
                                type: "gridtable",
                                name: "gridpodtl",
                                additembtn: true,
                                gridopt: {
                                    url: "api/mm/grid/purchasedtl",
                                    urldelete: '',
                                    urlsave: '',
                                    name: "gridpodtl",
                                    //params: data,
                                    hideshowrecord: true,
                                    hidepagination:true,
                                    key: "KdSupp",
                                    datatable: false,
                                    initrow: 5,
                                    isinit: true,
                                    filter: true,
                                    editonhover: false,
                                    actioncol: [{ type: "edit" },
                                                { type: "save", hide: true },
                                                { type: "delete" },
                                                { type: "view" },
                                                { type: "cancel", hide: true }
                                    ],
                                    columns: [
                                            {
                                                mData: "KdBrg", sTitle: "Kode Barang", colwidth: "250px", control: {
                                                    name: "KdBrg", type: "lkp", placeholder: "Kd Brg",
                                                    readonly: true, modal: "materialmodal", btncolor: "red", btnicon: "search",
                                                    callback: refreshsrcmaterial
                                                }
                                            },
                                            {
                                                mData: "NmBrg", sTitle: "Nama Barang"
                                            },
                                            {
                                                mData: "Qty", sTitle: "Qty", colwidth: "100px", control: {
                                                    type: "txt", placeholder: "Nomor Po"
                                                }
                                            },
                                            {
                                                mData: "UnitDsc", sTitle: "Satuan", colwidth: "100px"
                                            }
                                    ],
                                    //ondelete: griddelete,
                                    //onsave: gridsave,
                                    oncancel: function (rw, rslt) {
                                        if (rslt.length > 0) {
                                            console.log(rw);
                                            console.log(rslt);
                                            var td = $('td', rw);
                                            console.log(rslt[0].KdBrg);
                                            td[0].innerHTML = rslt[0].KdBrg;
                                            td[1].innerHTML = rslt[0].NmBrg;
                                            td[2].innerHTML = rslt[0].Qty;
                                            td[3].innerHTML = rslt[0].UnitIdDsc;
                                        }

                                    },
                                    ondone: griddone

                                }
                            }
                    ]

                }
            ]
        },
        {
            type: "row",
            controls: [
                {
                    type: "portlet", mustvalid: true, name: "pnlInput", color: "purple", title: "Manage Supplier", icon: "home", hidetool: ["remove", "config"], hidden: true,
                    body: [

                            {
                                type: "formgroup",
                                controls: [
                                       {
                                           name: "KdPo", type: "textbox", label: "Nomor PO", cls: "col-md-5",
                                           placeholder: "Nomor Po", required: true, minlength: 5
                                       }
                                ]
                            },
                            {
                                type: "formgroup",
                                controls: [
                                        {
                                            name: "KdSup", type: "textbox", label: "Supplier", cls: "col-md-5",
                                            placeholder: "Supplier", required: true
                                        }
                                ]
                            },
                            {
                                type: "formgroup",
                                controls: [
                                    {
                                        name: "Estimasi", txtcls: "input-inline input-xxsmall numeric", type: "textbox", label: "Estimasi", cls: "col-md-5",
                                        placeholder: "Estimasi", maxlength: 3, required: true, helptext: "Hari"
                                    }
                                ]
                            },
                            {
                                type: "formgroup",
                                controls: [
                                        {
                                            name: "PoDesc", type: "textbox", lang: true, label: "Keterangan",
                                            cls: "col-md-5", placeholder: "Keterangan"
                                        }
                                ]
                            },
                            {
                                type: "formgroupbtn",
                                controls: [
                                        { name: "btnNext", type: "btn", color: "blue", text: "Next", icon: "hand-o-right", onClick: refreshgriddetail }

                                ]
                            }
                            
                    ]
                }
            ]
        }
        ]
    }


    var widget = new PageEngine(opt);
    widget.render(init);


    function init() {
        widget.PopulateCombo({ nmspc: "bank", name: "BankId" })
        widget.PopulateCombo({ uri: 'api/APIcom/comboarea/p/', name: "Province" })

        //widget.gridsupplier.gridopt.filter = false;
        widget.gridsupplier.reload();
        widget.gridpodtl.reload();

        //refreshgrid();
     

     
        
        //widget.showonlyportlet("pnlInput");
        //widget.showbuttontoolbar("#btnSave,#btnCancel");


    }


    function create() {
        console.log('uuuu');
        //widget.gridsupplier.gridopt.filter=false;
        console.log(widget.gridsupplier.gridopt.filter);
        widget.gridsupplier.gridopt.filter = false;
        console.log(widget.gridsupplier.gridopt.filter);
        
        //widget.gridsupplier.hide();
        //widget.fgsatu.hide();
        //widget.fgsatu.class('yanu');
        //widget.fgsatu.grid();
        //widget.fgsatu.hide();

        return;
        iscreate = true;
        widget.clearcontrol("#pnlInput");
        widget.showonlyportlet("pnlInput");
        widget.showbuttontoolbar("#btnSave,#btnCancel");
        widget.readonly({ name: "#KdSupp", readonly: false });
    }

    function edit() {
        widget.fgsatu.show();
        return;
        var rows = $("#gridsupplier").dataTable().fnGetNodes();
        var cb = $("#gridsupplier input:checked");
        if (cb.length == 0) {
            widget.showmessage([{ type: "error", msg: "please select data first", title: "error edit" }]);
        }
        else if (cb.length > 1) {
            widget.showmessage([{ type: "error", msg: "multiple edit is not active yet", title: "error edit" }])
        }
        else {
            EditItem(cb.val());
        }
    }
    function deleted() {
        widget.Confirm({
            title: "Delete Supplier",
            msg: "<p>Anda yakin ingin menghapus supplier?</p>",
            ok: function () {
                var rslt = widget.submit(widget.getbaseurl() + 'scp/mdm/supplier/delete',
                    { cb: JSON.stringify(widget.getSelectedGridRow("gridsupplier")) }, true);
                if (rslt.success) {
                    widget.showmessage([{ msg: "data has been deleted", title: "Done", type: "success" }]);
                }
                refreshgrid();
            }
        });


    }

    function save() {
        widget.restorerowgrid(optdtl);
        return;
        if (!widget.isvalid()) {
        }
        else {
            var data = $("#pnlInput").serializeObject();
            data.iscreate = (iscreate ? iscreate : 0);
            var rslt = widget.submit(widget.getbaseurl() + 'scp/mdm/supplier/save', data, true);
            if (rslt.success) {
                widget.showmessage([{ msg: "data saved successfully", title: "Done", type: "success" }]);
            }
            else {
                widget.showmessage([{ msg: "Failed to Save: " + rslt.message, title: "Error", type: "error" }]);
            }
            widget.showonlyportlet('pnlfilter');
            widget.showbuttontoolbar("#btnCreate,#btnEdit,#btnDelete");
            refreshgrid();
        }
    }

    function cancel() {
        widget.showonlyportlet("pnlfilter");
        widget.showbuttontoolbar("#btnCreate,#btnEdit,#btnDelete");
    }

    function adddetail() {

    }

    function EditItem(data) {
        console.log(data);
        iscreate = false;
        widget.clearcontrol("#pnlInput");
        widget.populatedata({ url: "api/mdm/apisupp/mstsupplier/" + data }, "#pnlInput");
        widget.showonlyportlet("pnlInput");
        widget.showbuttontoolbar("#btnSave,#btnCancel");
        widget.readonly({ name: "#KdSupp" });
    }

    function refreshgrid() {
        var data = $("#pnlfilter").serializeObject();
        widget.reloadgrid({
            url: "api/mdm/grid/supplier",
            name: "gridsupplier",
            checkbox: true,
            params: data,
            key: "KdSupp",
            filter: true,
            columns: [
            { mData: "KdSupp", sTitle: "Kode Supplier " },
            { mData: "NmSupp", sTitle: "Nama Supplier" },
            { mData: "Phone1", sTitle: "Telp" },
            { mData: "Email", sTitle: "Email" },
            { mData: "Addr", sTitle: "Alamat" }

            ],
            callback: function (data) {
                EditItem(data[1]);
            }
        });
    }


    function griddone() {
        //var bd=$("#gridpodtl tbody");
        //$(bd).empty();
        //$(bd).append('<tr class="odd"><td class=" ">Pocarisweat</td><td class=" ">Pocarisweat</td><td class=" ">Pocarisweat</td><td class=" ">Pocarisweat</td></tr>');
        //$(bd).append('<tr class="even"><td class=" ">cocacola</td><td class=" ">cocacola</td><td class=" ">cocacola</td><td class=" ">cocacola</td></tr>');

        $("#gridpodtl tbody tr").dblclick(function (e) {
            console.log('222');
        });

    }

    function refreshsrcmaterial(rw) {

        var data = {};//$("#pnlfilter").serializeObject();

        widget.reloadgrid({
            url: "api/mdm/grid/barang",
            //checkbox: true,
            name: "modalgridmaterial",
            params: data,
            key: "uname",
            filter: true,
            columns: [
            { mData: "KdBrg", sTitle: "Kode Barang " },
            { mData: "NmBrg", sTitle: "Nama Barang" },
            { mData: "BCode", sTitle: "Barcode" },
            { mData: "TypeBrgDsc", sTitle: "Tipe Barang" },
            { mData: "MinStock", sTitle: "Min. Stok" },
            { mData: "InitStock", sTitle: "Stok" },
            { mData: "UnitIdDsc", sTitle: "Satuan" }
            ],
            rwdblclick: function (data) {
                var ltd = $('td', rw);
                var inpt = $(ltd[0]).find('input');
                if (inpt.length > 0) {
                    $(inpt).attr('value', data[0]);
                }
                else {
                    $('td:first', rw).html(data[0]);
                }

                $(ltd[1]).html(data[1]);
                $(ltd[3]).html(data[6]);




                $("#btnClose_materialmodal").click();
            }
        });
    }

    function refreshgriddetail() {
        var data = $("#pnlfilter").serializeObject();
        optdtl.params = data;
        widget.reloadgrid(optdtl);
    }



});